package com.hctforgreen.greenservice.model;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.hctforgreen.greenservice.model.BookListEntity.BookEntity;
import com.hctforgreen.greenservice.utils.HctConstants;

/**
 * 调试维修栏目信息
 * 
 * @author Tallen Zhai
 * 
 */
public class ColumnListEntity extends BaseEntity {

	private static final long serialVersionUID = -3863539524920658048L;

	public ArrayList<ColumnEntity> dataList = null;

	public String statusCode = "";
	public String msg = "";
	public final static String STATUS_CODE = "statusCode";
	public final static String MSG = "msg";
	private final static String COLUMNS = "columns";

	public static ColumnListEntity parse(JSONObject jsonObject, Context activity)
			throws JSONException {
		ColumnListEntity bean = new ColumnListEntity();
		if (jsonObject.has(STATUS_CODE)) {
			bean.statusCode = jsonObject.getString(STATUS_CODE);
		}
		if (jsonObject.has(MSG)) {
			bean.msg = jsonObject.getString(MSG);
		}
		/**
		 * 解析columns节点
		 */
		if (jsonObject.has(COLUMNS)) {

			JSONArray jsonArray = jsonObject.getJSONArray(COLUMNS);

			bean.dataList = new ArrayList<ColumnEntity>();
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject object = jsonArray.getJSONObject(i);
				ColumnEntity entity = ColumnEntity.parse(object, activity);
				bean.dataList.add(entity);
			}

		}

		return bean;
	}

	public static class ColumnEntity extends BaseEntity {

		private static final long serialVersionUID = 4388606491690604123L;

		public String icon = "";
		public String id = "";
		public String name = "";

		public final static String ICON = "icon";
		public final static String ID = "id";
		public final static String NAME = "name";

		// =========页面中才用的到=========
		public boolean isOff = true;

		public List<BookEntity> dataList = null;

		public static ColumnEntity parse(JSONObject jsonObject, Context activity)
				throws JSONException {
			ColumnEntity bean = new ColumnEntity();
			if (jsonObject.has(ID)) {
				bean.id = jsonObject.getString(ID);
				bean.icon = buildIconUrl(bean.id);
			}
			if (jsonObject.has(NAME)) {
				bean.name = jsonObject.getString(NAME);

			}

			return bean;
		}

		private static String buildIconUrl(String columnId) {

			return HctConstants.NET_URL + HctConstants.NET_SOURCE_MOBILE_PATH
					+ HctConstants.GET_BOOK_TYPE_COLUMN_PIC_ACTION
					+ HctConstants.NET_BOOK_TYPE_COLUMN_ID_HEADER + columnId;
		}
	}
}
